2022/01/[路飞][LeetCode]145_二叉树的后序遍历/index

看一百遍美女,美女也不一定是你的。但你刷一百遍算法,知识就是你的了~~

谁能九层台,不用累土起!

题目地址

题目

给定一个二叉树,返回它的 后序 遍历。

示例:

1
2
3
4
5
6
7
8
输入: [1,null,2,3]  
1
\
2
/
3

输出: [3,2,1]

解题思路

  • 我们首先要知道后序遍历的规则,输出顺序左 -> 右 -> 根
  • 按顺序进行遍历插入数组

解题代码

1
2
3
4
5
6
7
8
9
10
11
var postorderTraversal = function(root) {
let res = []
let after_order = (root)=>{
if(root==null) return
after_order(root.left)
after_order(root.right)
res.push(root.val)
}
after_order(root)
return res
};

如有任何问题或建议,欢迎留言讨论!

文章作者: Joker
文章链接: https://qytayh.github.io/2022/01/[%E8%B7%AF%E9%A3%9E][LeetCode]145_%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86/index/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Joker's Blog